package com.yazi.mxz.person.common;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.service.CompanyService;
import com.yazi.mxz.common.service.MemberService;
import com.yazi.mxz.common.service.StoreService;

public class BaseController extends com.yazi.mxz.common.controller.BaseController {

	@Resource
	MemberService memberService;
	
	@Resource
	StoreService storeService;
	
	@Resource
	CompanyService companyService;
	
	
	@ExceptionHandler({BindException.class})
	@ResponseBody
    public StatusResult handleBindException(BindException ex) {
		StatusResult jsonResult = new StatusResult();
		BindingResult br = ex.getBindingResult();
		jsonResult.setErrorMessage("1002", br.getFieldError().getDefaultMessage());
        return jsonResult;
    }
	/*
	@ExceptionHandler(Exception.class)
	@ResponseBody
    public StatusResult handleException(Exception ex) {
		ex.printStackTrace();
		StatusResult jsonResult = new StatusResult();
		jsonResult.setErrorMessage(ex.getMessage());
        return jsonResult;
    }
	*/
	
	public Member getMember() {
		Subject subject = SecurityUtils.getSubject();
		Principal pc = (Principal) subject.getPrincipal();
		return null != pc ? pc.getMember() : null;
	}

	public Long getMemberId() {
		if (null == getMember())
			return null;
		return getMember().getMemberId();
	}

	public Member getMember(boolean refresh) {
		if (!refresh)
			return getMember();
		if (null == getMemberId())
			return null;
		return memberService.one(getMemberId());
	}
	
	public Company getCompany() {
		Subject subject = SecurityUtils.getSubject();
		Principal pal = (Principal) subject.getPrincipal();
		return null != pal ? pal.getCompany() : null;
	}

	public Long getCompanyId() {
		if (null == getCompany())
			return null;
		return getCompany().getCompanyId();
	}

	public Company getCompany(boolean refresh) {
		if (!refresh)
			return getCompany();
		if (null == getCompanyId())
			return null;
		return companyService.one(getCompanyId());
	}

	public Store getStore() {
		Subject subject = SecurityUtils.getSubject();
		Principal pal = (Principal) subject.getPrincipal();
		return null != pal ? pal.getStore() : null;
	}

	public Long getStoreId() {
		if (null == getStore())
			return null;
		return getStore().getStoreId();
	}

	public Store getStore(boolean refresh) {
		if (!refresh)
			return getStore();
		if (null == getStoreId())
			return null;
		return storeService.one(getStoreId());
	}

}
